
# $Id: Node.pm 149 2013-04-13 05:42:04Z chengxin.cai $

use strict;

package SQConfig::Node;

use Switch;
use Carp;

use PerlUtil qw( gen_getter gen_setter );

use lib '/opt/hp/nv/lib/perl';
use instance ':all';

sub import {
    my $package = shift;
    $package =~ s/.*:://;

    ##export the constructor
    no strict 'refs';
    my ($caller) = caller;
    *{ $caller . '::' . $package } = \&{$package};

    gen_getter( __PACKAGE__, 'name' );
    gen_getter( __PACKAGE__, 'id' );
    gen_getter( __PACKAGE__, 'cores' );
    gen_getter( __PACKAGE__, 'processors' );
    gen_getter( __PACKAGE__, 'roles' );
    gen_getter( __PACKAGE__, 'cabno' );
    gen_getter( __PACKAGE__, 'as_string' );
}

sub Node {
    my ($line) = @_;

    my %attr_of =  map { split /=/ } split /;/, $line;

    my $name       = $attr_of{'node-name'};
    my $id         = $attr_of{'node-id'};
    my $cores      = $attr_of{cores};
    my $processors = $attr_of{processors};
    my $roles      = $attr_of{roles};
    my $cabno      = get_node_cabno( $attr_of{'node-id'} );

    bless sub {
        my $op = shift;

        switch ($op) {
            case 'name'       { return $name; }
            case 'id'         { return $id; }
            case 'cabno'      { return $cabno; }
            case 'cores'      { return $cores; }
            case 'processors' { return $processors; }
            case 'roles'      { return $roles; }
            case 'as_string'  { return sprintf "node-id=%s;node-name=%s;"
                                             . "cores=%s;processors=%s;roles=%s",
                                            $id, $name, $cores, $processors, $roles;
                              }

            else             { croak "Invalid operation $op"; }
        }
    }, __PACKAGE__;
}

use overload q{""} => \&as_string;

1;

